package com.demo.springMvcDemo.web;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import com.demo.springMvcDemo.model.Employee;
import com.demo.springMvcDemo.service.IEmployeeService;

@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {

	@Resource
	// @Autowired
	private IEmployeeService service;

	// private IEmployeeService service = new EmployeeServiceImpl();
	// 查询所有的
	@RequestMapping(value = "/employee", method = RequestMethod.GET)
	public String employee(Model model) {

		List<Employee> list = service.queryList();
		model.addAttribute("list", list);
		// return UrlBasedViewResolver.FORWARD_URL_PREFIX + "employee/employee";
		return "employee/employee";

	}

	// 添加先 调到insert视图
	@RequestMapping(value = "/toAdd", method = RequestMethod.GET)
	public String addPage() {
		return "employee/insert";// 服务器跳转 不用加forward

	}

	// 添加
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public String save(Employee employee) throws IOException {
		service.insertObject(employee);
		// return UrlBasedViewResolver.FORWARD_URL_PREFIX+"/employee()";
		return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/employee/employee";// 客户端跳转
																				// 加redict
	}

	// 修改 先根据id查询一条信息
	@RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
	public String queryObject(Integer id, Model model) {
		Employee employee = service.queryObject(id);
		model.addAttribute("em", employee);
		return "employee/update";
	}

	// 修改
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String save1(Employee employee) {
		service.updateObject(employee);
		return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/employee/employee";
	}

	// 删除
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String deleteObject(Integer id) {
		service.deleteObject(id);
		return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/employee/employee";
	}
}
